export function setToken(token: string) {
  localStorage.setItem('token', token)
}

export function setUserInfo(userInfo: any) {
  localStorage.setItem('userInfo', JSON.stringify(userInfo))
}
export function hasRootPermission() {
  const userInfo = JSON.parse(<string>localStorage.getItem('userInfo'))
  if (userInfo.is_super_admin) {
    return true
  }
}
export function hasTenantPermission() {
  const userInfo = JSON.parse(<string>localStorage.getItem('userInfo'))
  if (userInfo.is_super_admin || userInfo.is_tenant_admin) {
    return true
  }
  if (userInfo.permissions === 'None') {
    return false
  } else {
    const permissionList = JSON.parse(userInfo?.permissions.replace(/'/g, '"'))
    for (let item of permissionList) {
      if (item.split(':')[1] === '2') {
        return true
      }
    }
  }
  return false
}

export function hasAppPermission(appInfo) {
  const userInfo = JSON.parse(<string>localStorage.getItem('userInfo'))
  if (userInfo.is_super_admin || userInfo.is_tenant_admin) {
    return true
  }
  if (userInfo.permissions === 'None') {
    return false
  } else {
    const permissionList = JSON.parse(userInfo?.permissions.replace(/'/g, '"'))
    for (let item of permissionList) {
      if (item.split(':')[0] === appInfo.id) {
        return true
      }
    }
  }
  return false
}
